package com.apache.bid.controller.admin;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultMsg;
import com.apache.bid.controller.BaseSupperAction;
import com.apache.bid.entity.BidApply;
import com.apache.bid.entity.BidItemVip;
import com.apache.bid.manager.BidApplyManager;
import com.apache.bid.manager.BidItemVipManager;
import com.apache.bid.manager.UnityBaseManager;
import com.apache.cache.util.Validator;

@Controller
@RequestMapping("/admin/vip")
public class BidItemVipAction extends BaseSupperAction<BidItemVip> {

	@Autowired
	private BidItemVipManager bidItemVipManager;

	@Autowired
	private BidApplyManager bidApplyManager;

	@Override
	protected UnityBaseManager<BidItemVip> getBaseManager() {
		return bidItemVipManager;
	}

	@Override
	protected String getCommBoxValue(BidItemVip item, String type) {
		if ("id".equals(type)) {
			return item.getVipId();
		} else {
			return "Vip类型：" + item.getVipType() + ";Vip级别：" + item.getVipLevel();
		}
	}

	@Override
	public ResultMsg saveInfo(HttpServletRequest request, HttpServletResponse response, BidItemVip itemVip) {
		ResultMsg rmsg = new ResultMsg("T", "新增成功！");
		String userEname = itemVip.getUserEname();
		String itemId = itemVip.getItemId();
		String sessionId = request.getParameter("sessionId");
		if (Validator.isNull(userEname) || Validator.isNull(itemId) || Validator.isNull(sessionId)) {
			rmsg.setFlag("F");
			rmsg.setMsg("参数userEname,itemId,sessionId不能为空！");
			return rmsg;
		}

		//校验专厅或者单标的是否有此竞买人
		ParamsVo<BidApply> voApply = new ParamsVo<BidApply>();
		BidApply ba = new BidApply();
		ba.setUserName(userEname);
		if (!"0".equals(sessionId)) {
			ba.setSessionId(sessionId);
		} else {
			ba.setItemId(itemId);
		}

		voApply.setMethodKey("singleApplyByAttr");
		voApply.setObj(ba);
		BidApply bidApply = (BidApply) bidApplyManager.execute(voApply);
		if (bidApply == null) {
			rmsg.setFlag("F");
			rmsg.setMsg("没有此竞买人！");
			return rmsg;
		}

		//竞买人vip表判重
		ParamsVo<BidItemVip> vo = new ParamsVo<BidItemVip>();
		vo.setMethodKey("checkUserEname");
		vo.setObj(itemVip);
		Object result = bidItemVipManager.execute(vo);
		if ("true".equals(result.toString())) {
			rmsg.setFlag("F");
			rmsg.setMsg("登录名重复！");
			return rmsg;
		}

		vo = this.getParamsVo(request);
		vo.setObj(itemVip);
		String vipId = bidItemVipManager.saveInfo(vo);
		if (Validator.isNull(vipId)) {
			return rmsg = new ResultMsg("F", "发生异常！");
		}

		return rmsg;
	}
}
